package link.anyauto.j2m.custview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import link.anyauto.j2m.R;
import link.anyauto.j2m.util.ImageUtils;

/**
 * Created by voyo on 2016/5/7.
 */
public class SimpleImageView extends View {
    public SimpleImageView(Context context) {
        this(context, null, 0);
    }

    public SimpleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setAntiAlias(true);
        initAttrs(attrs);
    }

    void initAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray arr = null;
            try {
                arr = getContext().obtainStyledAttributes(attrs, R.styleable.SimpleImageView);
                drawable = arr.getDrawable(R.styleable.SimpleImageView_src);
                measureDrawable();
            } finally {
                if (arr != null) {
                    arr.recycle();
                }
            }
        }
    }

    void measureDrawable() {
        if(drawable == null) {
            throw new RuntimeException("drawable不能为空。");
        }
        width = drawable.getIntrinsicWidth();
        height = drawable.getIntrinsicHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(measuredWidth(wMode, width), measuredHeight(hMode, height));
    }

    int measuredWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                this.width = width;
                break;
        }
        return this.width;
    }

    int measuredHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                this.height = height;
                break;
        }
        return this.height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(drawable == null) {
            return;
        }
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        canvas.save();
        canvas.rotate(90);
        paint.setColor(Color.YELLOW);
        paint.setTextSize(30);
        canvas.drawText("AngelaBaby", getLeft() + 50, getTop() - 50, paint);
        canvas.restore();
//        canvas.drawBitmap(ImageUtils.drawableToBitmap(drawable), getLeft(), getTop(), paint);
    }

    Paint paint;
    Drawable drawable;
    int width;
    int height;
}
